/**
 * 检查患者与医生的未完成订单信息
 * 引用commonService.js,并直接在业务模块调用该方法，返回true，则return false;
 * @param {Object} userid
 * @param {Object} doctorid
 */
function checkUnfinishedOrder(userid, $doctorid) {
	var result = true;
	if($doctorid == null) {
		mui.alert("医生id不能为空!");
		return false;
	}
	var params = {
		userid: userid == null ? USERID : userid,
		doctorid: $doctorid
	}
	utilModule.get({
		url: "consultOrderPay/queryUnfinishedOrderByUid",
		data: params,
		needJson: false,
		async: false,
		success: function(responseObject) {
			/**如果有未完成信息，则提示跳转聊天界面 **/
			if(utilModule.checkUndefined(responseObject.id) != "") {
				result=false;
				console.log(responseObject);
				var btnArray = ['否', '是'];
				mui.confirm('您有与该医生有一条未完成咨询记录，去咨询？', '提示', btnArray, function(e) {
					if(e.index == 1) {
						invokeIm(responseObject, $doctorid);
					}
				});
				
			}
		}
	});
	return result;

}

/**
 *  获取医生基本信息并跳转IM
 * @param {Object} data
 */
function invokeIm(data, $doctorid) {
	var orderid = data.id;
	//咨询类型：1:快速提问      2:图文   3:用药咨询
	 // 图文：2   用药: 5
	var orderType = data.orderType;
	if(orderType==2){
		payStatus = 2
	}
	if(orderType==3){
		payStatus = 5
	}
	var params = {
		"doctorId": $doctorid
	};
	utilModule.post({
		url: "doctor/doctorDetail",  //医生详情页
		data: params,
		needJson: false,
		async: false,
		success: function(response) {
			var result = response;
			if(result != null) {
				var doctorInfo = result.doctor;
				var doctorObject = {};
				var usernameIM = DOCTOR_IM_PREFFIX + doctorInfo.id;
				
				doctorObject.doctorid = doctorInfo.id;
				doctorObject.usernameIM = usernameIM;
				doctorObject.avatar = doctorInfo.avatar;
				doctorObject.realname = doctorInfo.realname;
				doctorObject.deptName = doctorInfo.deptName;
				utilModule.setStorage(usernameIM, JSON.stringify(doctorObject));
				
				mui.toast("正在跳转咨询界面....");
				console.log("医生信息:" + utilModule.getStorage(usernameIM));
				//支付成功跳转im聊天界面  
				location.replace("../inquiry/inquiry-service.html?usernameIM=" + usernameIM + "&orderid=" + orderid + "&payStatus="+payStatus+"&doctorid=" + doctorInfo.id);
			}
		}
	});
}
 